package com.buss.FrontController;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.buss.artcomments.service.BnsArticleCommentsServiceI;
import com.buss.log.service.BnsLogServiceI;
import com.buss.shuoshuo.service.BnsShuoshuoServiceI;
import com.buss.user.entity.BnsUsersInfoEntity;
import com.buss.user.entity.UserLoginLog;

/**
 * 前台收藏控制层
 * @author hlg
 * @Date 2016-09-29
 */
@Scope("prototype")
@Controller
@RequestMapping("/collectController")
public class CollectController extends BaseController {
	private static final Logger logger = Logger.getLogger(CollectController.class);

	@Autowired
	private BnsShuoshuoServiceI bnsShuoshuoService;
	@Autowired
	private BnsLogServiceI bnsRizhiService;
	@Autowired
	private BnsArticleCommentsServiceI bnsRzCommentsService;
	@Autowired
	private SystemService systemService;
	
	
	/**
	 * 应全部页面
	 * @return
	 */
	@RequestMapping(params = "allCollect")
	public ModelAndView allCollect(Model model,HttpServletRequest request) {
		String pathStr = "front/allCollect";
		HttpSession session = request.getSession();
		BnsUsersInfoEntity userInfo = (BnsUsersInfoEntity) session.getAttribute("LOCAL_FRONT_USER");
		if (!StringUtil.isNotEmpty(userInfo)) {
			return new ModelAndView("front/login");
		}
		
		Long dataCount = 0L;
		int pageParam = 1;
		String page = request.getParameter("page");
		String collectTypes = request.getParameter("types");
		if (StringUtil.isEmpty(page)) {
			pageParam = 1;
		} else {
			pageParam = Integer.parseInt(page);
		}
		int mbId = userInfo.getUserId();
		dataCount = systemService.getCountForJdbcParam("select count(1) from bns_message where receive_id='" + mbId + "'", null);
		
		// 查询收藏  收藏类型 1-帖子，2-版块，3-群组，4-日志，5-相册
		if(collectTypes.equals("all")){	//全部
			collectTypes = "1,2,3,4,5";
		}
		
		String sql1 = "select * from bns_collects w where w.collect_userid='"+ mbId+ "' and w.collect_type in ("+ collectTypes+ ")";	
		List<Map<String,Object>> collectLists = systemService.findForJdbc(sql1, pageParam, 10);
		
		int pageCount = 0;
		if (dataCount.intValue() > 0 && dataCount.intValue() % 10 == 0) {
			pageCount = dataCount.intValue() / 10;
		} else {
			pageCount = dataCount.intValue() / 10 + 1;
		}
		
		//查找最近登录时间
		String querySql = "select MAX(w.loginTime) as loginTime from user_login_log w where w.userId='"+ mbId+ "' ";	
		List<Map<String,Object>> userloginLog = systemService.findForJdbc(querySql);
		
		model.addAttribute("pageCount", pageCount);
		model.addAttribute("collectLists", collectLists);
		model.addAttribute("userInfo", userInfo);
		model.addAttribute("lastLoginTime", userloginLog.get(0).get("loginTime"));
		
		model.addAttribute("backgroundColor", "allCollect");
		return new ModelAndView(pathStr);
	}
	
	
}
